from django.db import models


class Menu(models.Model):
    '''
    菜单表
    '''
    title=models.CharField(max_length=32,verbose_name='菜单标题',unique=True)
    icon=models.CharField(max_length=32,verbose_name='菜单图标')

    def __str__(self):
        return self.title

    class Meta:
        db_table='menu'
        verbose_name='菜单表'
        verbose_name_plural=verbose_name


class Permission(models.Model):
    '''
    权限表
    '''
    title=models.CharField(max_length=32,verbose_name='标题')
    url=models.CharField(max_length=64,verbose_name='权限url')
    name=models.CharField(max_length=64,verbose_name='url别名')
    parent=models.ForeignKey(to='Permission',verbose_name='父权限',null=True,blank=True,on_delete=models.CASCADE,limit_choices_to={'menu__isnull':False})
    menu=models.ForeignKey(to='Menu',verbose_name='菜单',null=True,blank=True,on_delete=models.CASCADE)
    def __str__(self):
        return self.title

    class Meta:
        db_table='permission'
        verbose_name='权限表'
        verbose_name_plural=verbose_name


class Role(models.Model):
    '''
    角色表
    '''
    title=models.CharField(max_length=32,verbose_name='角色名称')
    permissions=models.ManyToManyField(to='Permission',blank=True,verbose_name='拥有的权限')

    def __str__(self):
        return self.title

    class Meta:
        db_table='role'
        verbose_name='角色表'
        verbose_name_plural=verbose_name


class User(models.Model):
    '''
    用户表
    '''
    name=models.CharField(max_length=32,verbose_name='用户名')
    password=models.CharField(max_length=64,verbose_name='密码')
    email=models.CharField(max_length=32,verbose_name='邮箱')
    roles=models.ManyToManyField(to=Role,blank=True,verbose_name='拥有的角色')

    def __str__(self):
        return self.name

    class Meta:
        abstract=True
        db_table='user'
        verbose_name='用户表'
        verbose_name_plural=verbose_name